﻿using UnityEngine;
using System.Collections;

public class MoveAreaScript : MonoBehaviour {

	GameObject[] waypoints;
	Vector2 xDif, zDif;

	void Start(){
		int children = transform.childCount;
		waypoints = new GameObject[children];
		for (int i = 0; i < children; i++) {
			GameObject wp = transform.GetChild(i).gameObject;
			WaypointScript wpScript = (WaypointScript) wp.GetComponent("WaypointScript");
			waypoints[wpScript.order] = wp;
		}
		xDif = getXDif();
		zDif = getZDif();
	}

	public Vector3 getRandomInsideArea(){
		Vector2 coords = getRandomXZCoords ();
		return new Vector3 (coords.x, 0, coords.y);
	}

	Vector2 getRandomXZCoords (){
		float x, z;
		x = Random.Range (xDif.x, xDif.y);
		z = Random.Range (zDif.x, zDif.y);
		return new Vector2 (x, z);
	}

	bool LinesIntersect(Vector2 A, Vector2 B, Vector2 C)
	{
		Vector2 D = new Vector2 (C.x, zDif.y + 1);
		Vector2 CmP = new Vector2(C.x - A.x, C.y - A.y);
		Vector2 r = new Vector2(B.x - A.x, B.y - A.y);
		Vector2 s = new Vector2(D.x - C.x, D.y - C.y);
		
		float CmPxr = CmP.x * r.y - CmP.y * r.x;
		float CmPxs = CmP.x * s.y - CmP.y * s.x;
		float rxs = r.x * s.y - r.y * s.x;

		if (CmPxr == 0f)
		{
			// Lines are collinear, and so intersect if they have any overlap
			
			return ((C.x - A.x < 0f) != (C.x - B.x < 0f))
				|| ((C.y - A.y < 0f) != (C.y - B.y < 0f));
		}
		
		if (rxs == 0f)
			return false; // Lines are parallel.
		
		float rxsr = 1f / rxs;
		float t = CmPxs * rxsr;
		float u = CmPxr * rxsr;
		
		return (t >= 0f) && (t <= 1f) && (u >= 0f) && (u <= 1f);
	}

	Vector2 getXDif(){
		float min = Mathf.Infinity;
		float max = Mathf.NegativeInfinity;
		foreach(GameObject obj in waypoints){
			if(obj.transform.position.x > max){
				max = obj.transform.position.x;
			}
			if(obj.transform.position.x < min){
				min = obj.transform.position.x;
			}
		}
		return new Vector2 (min, max);
	}

	Vector2 getZDif(){
		float min = Mathf.Infinity;
		float max = Mathf.NegativeInfinity;
		foreach(GameObject obj in waypoints){
			if(obj.transform.position.z > max){
				max = obj.transform.position.z;
			}
			if(obj.transform.position.z < min){
				min = obj.transform.position.z;
			}
		}
		return new Vector2(min, max);
	}

	public bool InsidePolygon(Vector2 p)
	{
		//float angle=0;
		int count = 0;
		Vector2 p1,p2;
		int n = waypoints.Length;
		
		for (int i=0;i<n;i++) {
			p1.y = waypoints[i].transform.position.z;// - p.y;
			p1.x = waypoints[i].transform.position.x;// - p.x;
			if(i != n-1){
				p2.y = waypoints[(i+1)].transform.position.z;// - p.y;
				p2.x = waypoints[(i+1)].transform.position.x;// - p.x;
			}else{
				p2.y = waypoints[0].transform.position.z;// - p.y;
				p2.x = waypoints[0].transform.position.x;// - p.x;
			}
			if(LinesIntersect(p1, p2, p)){
				count++;
			}
			//angle += Angle2D(p1.y,p1.x,p2.y,p2.x);
		}
		//Debug.Log (count);

		if (count %2 == 1) {
			return true;
		} else {
			return false;
		}
		/*if (angle == 0)
			return true;
		else
			return false;*/
	}

	float Angle2D(float x1, float y1, float x2, float y2)
	{
		float dtheta,theta1,theta2;
		
		theta1 = Mathf.Atan2(y1,x1);
		theta2 = Mathf.Atan2(y2,x2);
		dtheta = theta2 - theta1;
		while (dtheta > Mathf.PI)
			dtheta -= (2*Mathf.PI);
		while (dtheta < -Mathf.PI)
			dtheta += (2*Mathf.PI);
		
		return(dtheta);
	}
}
